import file from "@/common/tools/file.js"
function fileObject(){
	this.filePathName = '' //文件本地完整路径
	this.file_name = '' //临时文件名
	this.content_type = 'image/jpeg' //临时文件MIME类型
	this.file_size = 0 //临时文件大小(字节)
	this.file_hash = '' //临时文件hash值
	this.width = 0 //对象分辨率width
	this.height = 0 //对象分辨率heigth
	this.upload_type = 'chat'
	this.file_total = 0 //分片数
	this.duration = 0
	this.resource_type = 'image'
	
	this.FragmentMaximum = 1024 * 1024 * 8 //5MB分片
	
	/* 从参数构造 */
	this.from_parms = function(filePathName,fileName,fileMime,fileSize,fileHash,width,height,uploadtype,file_total,duration){
		this.filePathName = filePathName //文件本地完整路径
		this.file_name = fileName //临时文件名
		this.content_type = fileMime //临时文件MIME类型
		this.file_size = fileSize //临时文件大小(字节)
		this.file_hash = fileHash //临时文件hash值
		this.width = width //对象分辨率width
		this.height = height //对象分辨率heigth
		this.upload_type = uploadtype
		this.file_total = 0 //分片数
		this.duration = 0
		return this
	}
	/* 从文件构造 */
	this.from_file = async function(filePathName,uploadtype,type){
		console.log(filePathName);
		this.filePathName = filePathName
		this.upload_type = uploadtype
		let filePathParms = filePathName.split('/')
		let fileCompleteName = filePathParms[filePathParms.length - 1]//文件完整名
		this.file_name = fileCompleteName
		/* 获取文件信息 */
		try{
			let fileInfo = await file.getFileInfo(filePathName)
			this.file_size = fileInfo.size
			this.file_hash = fileInfo.digest
			// if(fileInfo.size > this.FragmentMaximum){
			// 	this.file_total = Math.ceil(fileInfo.size / this.FragmentMaximum);
			// }
			if(type == 'image'){
				this.resource_type = 'image'
				let imageInfo = await file.getImageInfo(filePathName)
				this.width = imageInfo.width
				this.height = imageInfo.height
				this.content_type = 'image/' + imageInfo.type //临时文件MIME类型
			}else if(type == 'video'){
				this.resource_type = 'video'
				let imageInfo = await file.getVideoInfo(filePathName)
				this.duration = Math.ceil(imageInfo.duration) 
				this.width = imageInfo.width
				this.height = imageInfo.height
				this.content_type = imageInfo.type //临时文件MIME类型
			}else if(type == 'audio'){
				this.resource_type = 'audio'
				let imageInfo = await file.getAudioInfo(filePathName)
				this.duration = Math.ceil(imageInfo.duration)
			}
		}catch(e){}
		return this;
	}
	
	this.toRequest = function(){
		return {
			file_name:this.file_name, //临时文件名
			content_type:this.content_type, //临时文件MIME类型
			file_size:this.file_size,//临时文件大小(字节)
			file_hash:this.file_hash, //临时文件hash值
			width:this.width,//对象分辨率width
			height:this.height, //对象分辨率heigth
			upload_type:this.upload_type,
			file_total:this.file_total,
			duration:this.duration,
			resource_type:this.resource_type
		}
	},
	this.toBuffer = async function(){
		// return this.filePathName
		let baseziyuan = await getBase64(this.filePathName)
		//转换为2进制流
		const base64String = baseziyuan.split(',')[1];
		const arrayBuffer = base64ToArrayBuffer(base64String)
		return arrayBuffer
	}
	/**文件切片 */
	this.toSliceBuffer = async function() {
		let arrayBuffer = await this.toBuffer()
		const result = []
		let index = 0
		for (let nowSize = 0; nowSize < arrayBuffer.byteLength; nowSize += this.FragmentMaximum) {
			result.push(arrayBuffer.slice(nowSize, nowSize + this.FragmentMaximum))
			index++
		}
		return result
	}
	
}

function getBase64(path) {
	return new Promise((resolve, reject) => {
		console.log('---',path);
		plus.io.resolveLocalFileSystemURL(path, function(entry) {
			entry.file(function(file) {
				var fileReader = new plus.io.FileReader();
				fileReader.onerror = function(e) { //读取文件出错。
					console.log(e);
					reject(getApp().$t("fileObject.js.readError"))
				};
				fileReader.onload = async function(e) { //读取文件结束。
					resolve(e.target.result)
				}
				fileReader.readAsDataURL(file);
			});
		});
	})
}

function base64ToArrayBuffer(base64) {
	const binaryString = atob(base64);
	const length = binaryString.length;
	const bytes = new Uint8Array(length);

	for (let i = 0; i < length; i++) {
		bytes[i] = binaryString.charCodeAt(i);
	}

	return bytes.buffer;
}

/**
 * 构建
 */
export default {
	from_parms(filePathName,fileName,fileMime,fileSize,fileHash,width,height,uploadtype,file_total,duration){
		let instance = new fileObject()
		return instance.from_parms(filePathName,fileName,fileMime,fileSize,fileHash,width,height,uploadtype,file_total,duration)
	},
	/* 从文件构造 */
	from_file(filePathName,uploadtype,type){
		console.log(filePathName,uploadtype,type);
		let instance = new fileObject()
		return instance.from_file(filePathName,uploadtype,type)
	},
}